//
// WebpageFu.cs
//
// Copyright (C) 2008 Lukas Lipka <lukaslipka@gmail.com>
//

using System;
using System.IO;
using System.Net;
using System.Xml;

namespace Dashboard.Util {
	
	public static class WebpageFu {

		public static StreamReader GetStream (Uri uri)
		{
			return GetStream (uri.ToString ());
		}

		public static WebResponse CreateHttpRequest (string url)
		{
			HttpWebRequest request = HttpWebRequest.Create (url) as HttpWebRequest;
			WebResponse response = request.GetResponse ();

			return response;
		}

		public static WebResponse CreateHttpRequest (Uri uri)
		{
			return CreateHttpRequest (uri.ToString ());
		}

		public static StreamReader GetStream (string url)
		{
			WebResponse response = CreateHttpRequest (url);

			Stream stream = response.GetResponseStream ();
			StreamReader reader = new StreamReader (stream);

			return reader;
		}

		public static string GetContent (Uri uri)
		{
			return GetContent (uri.ToString ());
		}

		public static string GetContent (string url)
		{
			WebResponse response = CreateHttpRequest (url);
			return GetContent (response);
		}

		public static string GetContent (HttpWebRequest request)
		{
			return GetContent (request.GetResponse ());
		}

		public static string GetContent (WebResponse response)
		{
			Stream stream = response.GetResponseStream ();
			StreamReader reader = new StreamReader (stream);

			string content = reader.ReadToEnd ();
			 
			reader.Close ();
			stream.Close ();
			response.Close ();
			
			return content;
		}

		public static XmlDocument GetXml (Uri uri)
		{
			return GetXml (uri.ToString ());
		}

		public static XmlDocument GetXml (string url)
		{
			WebResponse response = CreateHttpRequest (url);
			return GetXml (response);
		}

		public static XmlDocument GetXml (HttpWebRequest request)
		{
			return GetXml (request.GetResponse ());
		}

		public static XmlDocument GetXml (WebResponse response)
		{
			Stream stream = response.GetResponseStream ();

			XmlDocument document = new XmlDocument ();
			document.Load (stream);

			stream.Close ();
			response.Close ();

			return document;
		}
	}
}
